package com.netsdk.lht2.handle.support;

import com.netsdk.demo.util.sql.EventDto;
import com.netsdk.demo.util.sql.JdbcUtils;
import com.netsdk.demo.util.sql.Trajectory;
import com.netsdk.lht2.handle.EventHandle;
import com.netsdk.lht2.util.EventDtoUtil;
import com.netsdk.lht2.push.PushClient;
import com.netsdk.lht2.util.TrajectoryUtil;
import com.netsdk.lht2.util.ZbsetCache;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.enumeration.EM_CLASS_TYPE;
import com.sun.jna.Pointer;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/*
* 交通流量事件 处理
* */
public class EVENT_IVS_TRAFFIC_FLOWSTATE_HANDLE extends EventHandle {

    private final PushClient pushClient ;
    private final ExecutorService executorService = Executors.newScheduledThreadPool(4);

    public EVENT_IVS_TRAFFIC_FLOWSTATE_HANDLE() throws IOException {
        pushClient = new PushClient();
    }

    @Override
    public void handle(int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) throws UnsupportedEncodingException {
        System.out.println("交通流量事件, code = " + dwAlarmType);
        try {
            NetSDKLib.DEV_EVENT_TRAFFIC_FLOW_STATE msg = new NetSDKLib.DEV_EVENT_TRAFFIC_FLOW_STATE();
            ToolKits.GetPointerData(pAlarmInfo, msg);
            // 通道号
            String nChannelID = getChannelMapping(msg.nChannelID);
            System.out.println("nChannelID:" + msg.nChannelID);
            //相对事件时间戳(单位是毫秒)
            double pts = msg.PTS;
            //UTC时间 事件发生时间
            NetSDKLib.NET_TIME_EX UTC = msg.UTC;
            System.out.println("UTC:" + UTC.toStringTime());
            //事件id
            int nEventID = msg.nEventID;
            //流量状态数量
            int nStateNum = msg.nStateNum;
            //该事件触发的预置点
            int nPresetID = msg.stuIntelliCommInfo.nPresetID;
            //智能事件所属大类
            String emClassType = EM_CLASS_TYPE.getNoteByValue(msg.stuIntelliCommInfo.emClassType);
            //流量状态数量
            int nStopVehiclenum = msg.nStopVehiclenum;
            //流量状态数量
            int nDetectionAreaVehicleNum = msg.nDetectionAreaVehicleNum;
            System.out.println("车辆总数: "+ nDetectionAreaVehicleNum);

            List<EventDto> list = new ArrayList<>();

            for (int i = 0; i < nStateNum; i++) {
                System.out.println(msg.stuStates[i]);
                //车道流量
                int nums = msg.stuStates[i].dwFlow;
                System.out.println("车辆数:" + nums);
                // 有车流才生成轨迹
                if(msg.stuStates[i].dwFlow >0 ){
                    //交通流量事件，转换实体类
                    EventDto e  = EventDtoUtil.toEventDto(msg,msg.stuStates[i]);
                    // 重新设置 chennelId
                    String channelId = getChannelMapping(msg.nChannelID);
                    e.setChannel(channelId);
                    list.add(e);
                    //车辆轨迹设置
                    for (EventDto eventDto : list) {
                        eventDto.setState_num(nums+"");
                        JdbcUtils.insert(eventDto);
                    }
                    try {
                        // 生成车道轨迹
                        List<Map<String, Object>> zblist = ZbsetCache.queryBean( e.getChannel(), (msg.stuStates[i].nLane+1));
                        if (null != zblist && zblist.size() > 0) {
                            for (int w = 0; w < zblist.size(); w++) {
                                //限制最大过车量
                                //車少情況
                                List<Trajectory[]> nLaneData = new ArrayList<>();
                                for (int j = 0; j < nums; j++) {
                                    List<Trajectory[]> data = TrajectoryUtil.create(UTC.toString2(),1,channelId,zblist.get(w).get("nLaneIndex").toString());
//                                    for (Trajectory trajectory : trajectoryArr) {
//                                        count += JdbcUtils.insert(trajectory);
//                                    }
                                    nLaneData.addAll(data);
//                                    pushClient.push(UTC.toStringTime(),channelId,nLane,trajectoryArr);

                                }
                               //  JdbcUtils.updateTrajectory();
                                // 数据推送
                                pushClient.push2(UTC.toStringTime(),channelId,(msg.stuStates[i].nLane+1)+"",nLaneData);
                            }
                        }
                    }catch(Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
